﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;

namespace DAO
{
    public class DauThauDAO
    {
        DataProvider provider;

        public DauThauDAO()
        {
            provider = new DataProvider();
        }

        private string LayMaDauThauMoi()
        {
            string maDT = "DT";

            string sqlStatement = "SELECT COUNT(*) FROM DauThau";
            DataTable dt = provider.ExecuteQuery(sqlStatement);
            if (dt != null)
            {
                int noDT = Int32.Parse(dt.Rows[0][0].ToString()) + 1;
                int temp = noDT;
                int n = 0;
                while (temp > 0)
                {
                    temp = temp / 10;
                    n++;
                }
                while (n <= 11)
                {
                    maDT += "0";
                    n++;
                }
                maDT += noDT;
            }

            return maDT;
        }

        public int ThemDauThau(DauThau dauThau)
        {
            string sqlStatement = "INSERT INTO DauThau VALUES "
                + "('" + LayMaDauThauMoi() + "', N'" + dauThau.TenDauThau + "', '" + dauThau.NgayGioBatDau
                + "', '" + dauThau.NgayGioKetThuc + "', " + dauThau.SoLuongCungCap 
                + ", '" + dauThau.MaSanPham + "', " + dauThau.SoTienBatDau + ", " + dauThau.SoTienDanDau 
                + ", '" + dauThau.MaTaiKhoan + "', 0, 0)";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int CapNhatDauThau(DauThau dauThau)
        {
            string sqlStatement = "UPDATE DauThau SET TenDauThau=N'" + dauThau.TenDauThau
                + "', NgayGioBatDau='" + dauThau.NgayGioBatDau + "', NgayGioKetThuc='" + dauThau.NgayGioKetThuc
                + "', SoLuongCungCap=" + dauThau.SoLuongCungCap + ", MaSanPham='" + dauThau.MaSanPham
                + "', SoTienBatDau=" + dauThau.SoTienBatDau + ", SoTienDanDau=" + dauThau.SoTienDanDau
                + ", MaTaiKhoan='" + dauThau.MaTaiKhoan + "', DaDuyet=" + dauThau.DaDuyet
                + " WHERE MaDauThau=" + dauThau.MaDauThau;
            
            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int XoaDauThau(DauThau dauThau)
        {
            string sqlStatement = "UPDATE DauThau SET DaXoa=1 WHERE MaDauThau='" + dauThau.MaDauThau + "'";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public ArrayList LayDanhSachDauThau()
        {
            ArrayList arrDauThau = new ArrayList();
            try
            {
                string sqlStatement = "SELECT * FROM DauThau";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(sqlStatement);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DauThau item = new DauThau();

                    item.MaDauThau = dt.Rows[i]["MaDauThau"].ToString();
                    item.TenDauThau = dt.Rows[i]["TenDauThau"].ToString();
                    item.NgayGioBatDau = DateTime.Parse(dt.Rows[i]["NgayGioBatDau"].ToString());
                    item.NgayGioKetThuc = DateTime.Parse(dt.Rows[i]["NgayGioKetThuc"].ToString());
                    item.SoLuongCungCap = Int32.Parse(dt.Rows[i]["SoLuongCungCap"].ToString());
                    item.MaSanPham = dt.Rows[i]["MaSanPham"].ToString();
                    item.SoTienBatDau = float.Parse(dt.Rows[i]["SoTienBatDau"].ToString());
                    item.SoTienDanDau = float.Parse(dt.Rows[i]["SoTienDanDau"].ToString());
                    item.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                    item.DaDuyet = bool.Parse(dt.Rows[i]["DaDuyet"].ToString());
                    item.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());

                    arrDauThau.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrDauThau;
        }
    }
}
